package com.kdd.campus.sh.component;

import com.alibaba.fastjson.JSONObject;
import com.kdd.campus.common.entity.CommonResult;
import com.kdd.campus.common.entity.HttpCode;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Auther: fcp
 * @Date: 2020/4/27 09:48
 * @Description:
 */
public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        response.getWriter().println(JSONObject.toJSONString(CommonResult.failed(HttpCode.UNAUTHORIZED)));
        response.getWriter().flush();
    }
}
